package com.peaksport.pkms.learn.day21.helloworld.provider.controller;

import java.util.Date;

import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

import com.ibm.icu.math.BigDecimal;
import com.netflix.config.DynamicPropertyFactory;
import com.netflix.config.DynamicStringProperty;
import com.peaksport.pkms.learn.day21.helloworld.Gender;
import com.peaksport.pkms.learn.day21.helloworld.vo.GreetingResponse;
import com.peaksport.pkms.learn.day21.helloworld.vo.OrgUnit;
import com.peaksport.pkms.learn.day21.helloworld.vo.Person;

import io.swagger.annotations.ApiOperation;

@RestSchema(schemaId = "helloworldProvider")
@RequestMapping(path = "/peakinfo-provider/v0")
public class HelloService {

	private static final Logger LOGGER = LoggerFactory.getLogger(HelloService.class);
	@Autowired
	private RestTemplate restTemplate;
	/*private DynamicStringProperty sayHelloPrefix = DynamicPropertyFactory.getInstance()
			.getStringProperty("hello.sayHelloPrefix", "", new Runnable() {

				@Override
				public void run() {
					LOGGER.info("config[hello.sayHelloPrefix] changed to [{}]!", sayHelloPrefix.getValue());

				}
			});
			*/
	private DynamicStringProperty sayHelloPrefix = DynamicPropertyFactory.getInstance()
			.getStringProperty("hello.sayHelloPrefix", "",notifyConfigRefreshed()
			);

	@RequestMapping(path = "/hello", method = RequestMethod.GET)
	public String sayHello(@RequestParam(value = "name") String name) {
		//if (true) throw PKDevException.throwException("娃哈哈");
		return "hello8282 -  " + String.format("[%s]", sayHelloPrefix.getValue()) + name;
	}

	@GetMapping(path="/spring")
	public String getSpring() {
		ResponseEntity<String> responseEntity = restTemplate.getForEntity("cse://helloworld-consumer/helloworld-consumer/v0/hello?name=guo", String.class);
		return responseEntity.getBody();
		//return "aaa";
	}
	
	@GetMapping(path="/spring2",produces="application/json;charset=utf-8")
	public String getSpring2() {
		ResponseEntity<String> responseEntity = restTemplate.getForEntity("cse://hellomesher/spring/test2?name=guo", String.class);
		return responseEntity.getBody();
	}


	private Runnable notifyConfigRefreshed() {
		return () -> LOGGER.info("=============================config[hello.sayHelloPrefix] changed to [{}]!", sayHelloPrefix.getValue()); 
	}


	@ApiOperation(nickname="sayHello2", value = "测试方法重载sayHello")
	@RequestMapping(path = "/newhello/{name}/{pwd}", method = RequestMethod.GET)
	public String sayHello(@PathVariable(value = "name") String name, @PathVariable(value = "pwd") String pwd) {
		return "new hello0 " + name + "=" + pwd;
	}

	@RequestMapping(path = "/hello/getperson", method = RequestMethod.GET)
	public Person getPerson() {
		Person p = new Person();
		OrgUnit o = new OrgUnit();
		o.setId("orgId");
		o.setName("行政部");
		p.setOrgUnit(o);
		p.setGender(Gender.FEMAIL);
		p.setName("老王");
		BigDecimal b = new BigDecimal(4.5254);
		// b = b.setScale(2,BigDecimal.ROUND_HALF_UP);
		p.setAge(b);
		double companyAge = 32.52353;
		p.setCompanyAge(companyAge);
		;
		return p;
	}

	@PostMapping(path = "/greeting")
	public GreetingResponse greeting(@RequestBody Person person) {
		GreetingResponse greetingResponse = new GreetingResponse();
		if (Gender.MALE.equals(person.getGender())) {
			greetingResponse.setMsg("Hello, Mr. " + person.getName());
		} else {
			greetingResponse.setMsg("Hello, Mis. " + person.getName());
		}
		greetingResponse.setTimestamp(new Date());
		return greetingResponse;
	}

}
